﻿package Naruto{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class Attack extends Sprite{
		private var _stage : Stage = null;
		
		private var me:Naruto1; 			//主角
		public var aGenin:AGenin;			//普通敌人
		public var _aZabuza:AZabuza;		//再不斩
		private var flys1:Flys1;			//飞镖
		private var flys2:Flys1;
		public var flysspeed:Number; 	//飞镖速度
		public var angle:Number; 		//飞镖角度
		public var dx:Number;
		public var dy:Number;
		
		
		public function Attack(){
			flysspeed = 30;				//飞镖速度
			angle = 0;
			dx = 0;
			dy = 0;
		}
		public function MeStartAttack(stage:Stage,me:Naruto1):Flys1{
			flys1 = new Flys1();
			flys1.x = me.x;
			flys1.y = me.y;
			dx = mouseX-flys1.x;
			dy = mouseY-flys1.y;
			angle =  Math.atan2(dy,dx);
			flys1.rotation = angle * 180 / Math.PI; //飞镖角度
			
			this._stage = stage;
			addflys1();
			return this.flys1;
		}
		
		//普通敌人攻击
		public function AiStartAttack(stage:Stage,attacker:AGenin,me1:Naruto1):Flys1{ 
			//trace("Ai Start Attack");
			this.aGenin = attacker;
			this.me = me1;
			this.flys2 = new Flys1();
			this.flys2.x = this.aGenin.x;
			//trace(flys2.x);
			this.flys2.y = this.aGenin.y;
			flysspeed = 15;
			
			this.dx = this.me.x - this.flys2.x;
			this.dy = this.me.y - this.flys2.y;
			angle = Math.atan2(dy,dx);
			this.flys2.rotation = angle * 180 /Math.PI;
			this._stage = stage;
			this._stage.addEventListener(Event.ENTER_FRAME,AiFlysAttack);
			addflys2();
			return this.flys2;
		}
		//再不斩攻击
		public function BossStartAttack(stage:Stage,attacker:AZabuza,me1:Naruto1):Flys1{
			//trace("Ai Start Attack");
			this._aZabuza = attacker;
			this.me = me1;
			this.flys2 = new Flys1();
			this.flys2.x = this._aZabuza.x;
			//trace(flys2.x);
			this.flys2.y = this._aZabuza.y;
			flysspeed = 25;
			
			this.dx = this.me.x - this.flys2.x;
			this.dy = this.me.y - this.flys2.y;
			angle = Math.atan2(dy,dx);
			this.flys2.rotation = angle * 180 /Math.PI;
			this._stage = stage;
			this._stage.addEventListener(Event.ENTER_FRAME,AiFlysAttack);
			addflys2();
			return this.flys2;
		}
		
		public function AiFlysAttack(event:Event):void{
			if(this.flys2){
				var vx:Number = Math.cos(angle) * flysspeed;
				var vy:Number = Math.sin(angle) * flysspeed;
				this.flys2.x += vx;
				this.flys2.y += vy;
			}
			//trace(flys2.x);
		}
		public function getflys1(){
			return this.flys1;
		}
		public function addflys1(){
			this._stage.addChild(this.flys1);
		}
		public function getflys2(){
			return this.flys2;
		}
		public function addflys2(){
			this._stage.addChild(this.flys2);
		}
		public function removeflys1(){
			if(this._stage.contains(flys1)){
				this._stage.removeChild(this.flys1);
				this.flys1 = null;
			}
			return;
		}
		public function removeflys2(){
			if(this._stage.contains(this.flys2)){
				this._stage.removeChild(this.flys2);
				this.flys2 = null;			
			}
			return;
		}
	}	
}